<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item  label=" " prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入邮件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label=" " prop="consigneeName">
        <el-input
          v-model="queryParams.consigneeName"
          placeholder="请输入收件人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item  label=" " prop="emailType">
        <el-select v-model="queryParams.emailType" placeholder="请选择邮件类型" clearable>
          <el-option
            v-for="dict in dict.type.email_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eamp:email:remove']"
        >删除</el-button>

      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="邮件id" align="center" prop="emailId" />
<!--      <el-table-column label="邮件类型  1普通 2机密 3其他" align="center" prop="emailType" />-->
      <el-table-column label="邮件名称" align="center" prop="title" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="邮件类型" align="center" prop="emailType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.email_type" :value="scope.row.emailType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="收件人id" align="center" prop="consigneeId" />-->
<!--      <el-table-column label="附件地址" align="center" prop="file" />-->
<!--      <el-table-column label="正文内容" align="center" prop="contentText" />-->
      <el-table-column label="收件人" align="center" prop="consigneeName" />
      <el-table-column label="发件时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eamp:email:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eamp:email:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改邮件信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="true">
        <el-form-item label="邮件名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入邮件名称" disabled />
        </el-form-item>
        <el-form-item label="收件人" prop="consigneeName">
          <el-input v-model="form.consigneeName" placeholder="" disabled/>
        </el-form-item>
        <el-form-item label="邮件类型" prop="emailType">
          <el-select v-model="form.emailType" placeholder="请选择邮件类型" disabled>
            <el-option
              v-for="dict in dict.type.email_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件" prop="file" >
          <file-upload v-model="form.file"/>
        </el-form-item>
        <el-form-item label="正文内容" prop="contentText">
          <el-input v-model="form.contentText" placeholder="请输入正文内容" disabled/>
        </el-form-item>
<!--        <el-form-item label="发件人id" prop="addresserId">-->
<!--          <el-input v-model="form.addresserId" placeholder="请输入发件人id" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmail, getEmail, delEmail, addEmail, updateEmail } from "@/api/eamp/email";

export default {
  name: "Email",
  dicts: ['email_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 邮件信息表表格数据
      emailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        emailGenre: 2,
        delFig: 0,
        pageNum: 1,
        pageSize: 10,
        emailType: null,
        title: null,
        consigneeId: null,
        file: null,
        contentText: null,
        addresserId: null,
        consigneeName:null,
        ueId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询邮件信息表列表 */
    getList() {
      this.loading = true;
      listEmail(this.queryParams).then(response => {
        this.emailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        emailId: null,
        emailType: null,
        title: null,
        consigneeId: null,
        file: null,
        contentText: null,
        createTime: null,
        addresserId: null,
        ueId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.emailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加邮件信息表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.queryParams.emailId=row.emailId || this.ids
      this.queryParams.ueId=row.ueId
      getEmail(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.emailId != null) {
            updateEmail(this.form).then(response => {
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const emailIds = row.emailId || this.ids;
      this.$modal.confirm('是否确认删除邮件信息表id为"' + emailIds + '"的数据项？').then(function() {
        return delEmail(emailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eamp/email/export', {
        ...this.queryParams
      }, `email_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
